const query = require("./dbconn.js");
// 返回所有记录
exports.getall =  function(callback){
  query('SELECT * FROM `books`',function (err, results, fields) {
    if(err){
        console.log('数据查询失败');
        throw err;
    }    
    callback(results);
  });
}
// 返回指定记录
exports.get = function(bookid,callback){
    var arr = [];
    arr.push(bookid);   
    query('SELECT * FROM `books` WHERE `id` = ?', arr,function (err, results, fields) {
        if(err){
            console.log('数据查询失败');
            throw err;
        }        
        callback(results);
    });

}
// 增加一条记录
exports.add = function(rec,callback){   
    var  addSql = ' INSERT INTO `books`(`isbn`, `name`, `author`, `press`, `price`, `pubdate`) VALUES(?,?,?,?,?,?)';
    query(addSql,rec,function (err, results,fields) {
        if(err){
            console.log('[插入记录错误] - ',err.message);
            throw err;
        }       
        callback(results);
    });  
}
// 修改一条记录
exports.update = function(bookid,keys,values,callback){   
    var updateSql = 'UPDATE books SET ';
    console.log(keys);
    for ( var i = 0; i <keys.length; i++){
        if (i==keys.length-1)
            updateSql += keys[i] + '= ? ';
        else
            updateSql += keys[i] + '= ?,';        
    }
    updateSql +=' WHERE id = ?';
    values.push(bookid);
    query(updateSql,values,function (err, results,fields) {
        if(err){
            console.log('[修改记录错误] - ',err.message);
            throw err;
        }       
        callback(results);
    });  
}
// 删除一条记录
exports.del = function(bookid,callback){   
    var arr = [];
    arr.push(bookid);
    var  delSql = 'DELETE FROM books WHERE `id` = ?';
    query(delSql,arr,function (err, results,fields) {
        if(err){
            console.log('[删除记录错误] - ',err.message);
            throw err;
        }       
        callback(results);
    });  
}